﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using BUS;
using DTO;
namespace GUI
{
    public partial class FormBaoCaoDoanhThuTheoThang : Form
    {
        public class DoanhThuTheoThang
        {
            private int sSTT;
            private int sNgayTrongThang;
            private long sDoanhThu;
            private double sTile;
            public int STT
            {
                get { return sSTT; }
                set { sSTT = value; }
            }
            public int NgayTrongThang
            {
                get { return sNgayTrongThang; }
                set { sNgayTrongThang = value; }
            }
            public long DoanhThu
            {
                get { return sDoanhThu; }
                set { sDoanhThu = value; }
            }
            public double Tile
            {
                get { return sTile; }
                set { sTile = value; }
            }

        };
        
        string[] ChonThang = { "Chọn", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
        public FormBaoCaoDoanhThuTheoThang()
        {
            InitializeComponent();
            HoaDonBUS hdBUS = new HoaDonBUS();
            CbbchonThang.DataSource = ChonThang;
            cbbChonNam.DataSource = hdBUS.DanhSachNam();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }
        string path = "";
        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Chọn nơi lưu";
            sfd.Filter = "Excel WorkBook|*.xls";
            sfd.FileName = "Bao Cao.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void XuatRaFileExcel(string FileName)
        {
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }
            Excel._Application oApp;
            Excel.Worksheet oSheet;
            Excel.Workbook oBook;
            oApp = new Excel.Application();
            oBook = (Excel.Workbook)oApp.Workbooks.Add();
            oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
            // Tiểu Đề
            oSheet.Cells[1, 1] = "STT";
            oSheet.Cells[1, 2] = "Ngày Trong Tháng";
            oSheet.Cells[1, 3] = "Doanh Thu";
            oSheet.Cells[1, 4] = "Tỉ Lệ";
            int i = 2;
            foreach (DoanhThuTheoThang singleR in danhSachDoanhThu)
            {
                oSheet.Cells[i, 1] = i - 1;
                oSheet.Cells[i, 2] = singleR.NgayTrongThang;
                oSheet.Cells[i, 3] = singleR.DoanhThu;
                oSheet.Cells[i, 4] = singleR.Tile;
                i++;

            }
            oBook.SaveAs(FileName);
            oBook.Close();
            oApp.Quit();
        }
        // Do Có Xuất Ra File Excel cho nên phải khai báo toàn cục
        List<DoanhThuTheoThang> danhSachDoanhThu = new List<DoanhThuTheoThang>();
        long TongDoanhThu = 0;
        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
                int thang = CbbchonThang.SelectedIndex;
                int nam = Convert.ToInt32 (cbbChonNam.SelectedValue.ToString());
                int MaxNgay=0;
                try
                {
                    MaxNgay = DateTime.DaysInMonth(nam, thang);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Vui Lòng Chọn Tháng");
                    return;
                }
                
                int[] iNgay = new int[33];
                int j = 0;
                for (int i = 0; i < MaxNgay;i++ )
                {

                    iNgay[j] = i+1;
                    j++;

                }
                for (int i = 0; i < iNgay.Length - 1; i++)
                {
                    if(iNgay[i] !=0)
                    {
                        DoanhThuTheoThang singleRecord = new DoanhThuTheoThang();
                        TinhTienBUS tt = new TinhTienBUS();
                        singleRecord.STT = i + 1;
                        singleRecord.DoanhThu = tt.DoanhThuTheoThang(iNgay[i],thang,nam);
                        singleRecord.NgayTrongThang = iNgay[i];
                        singleRecord.Tile = 0;
                        danhSachDoanhThu.Add(singleRecord);
                        
                    }
                }
                foreach (DoanhThuTheoThang singleRecord in danhSachDoanhThu)
                {
                    TongDoanhThu = TongDoanhThu + singleRecord.DoanhThu;
                }
                foreach (DoanhThuTheoThang singleRecord in danhSachDoanhThu)
                {
                    singleRecord.Tile = Math.Round((double)(singleRecord.DoanhThu * 100) / TongDoanhThu, 2);
                }
            foreach( DoanhThuTheoThang singleRecord in danhSachDoanhThu )
            {
                dataGridView1.Rows.Add(singleRecord.STT, singleRecord.NgayTrongThang, string.Format("{0:#,##0 VND;(#,##0 VND);''}", singleRecord.DoanhThu), singleRecord.Tile);
            }
            textBox1.Text = string.Format("{0:#,##0 VND;(#,##0 VND);''}", TongDoanhThu);
            if(checkBox1.Checked)
            {
                XuatRaFileExcel(path);
            }
            MessageBox.Show("Báo Cáo Thành Công");
            danhSachDoanhThu.Clear();
            TongDoanhThu = 0;
            return;

            
        }

        private void FormBaoCaoDoanhThuTheoThang_Load(object sender, EventArgs e)
        {

        }
    }
}
